﻿// Warn.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "Warn.h"
#include <string>
#include "liwishell.h"
#include "Data.h"



// Warn 对话框

IMPLEMENT_DYNAMIC(Warn, CDialogEx)

Warn::Warn(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_Warn, pParent)
{

}

Warn::~Warn()
{
}

void Warn::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(Warn, CDialogEx)
	ON_BN_CLICKED(IDC_OpenWarn, &Warn::OnBnClickedOpenwarn)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// Warn 消息处理程序


BOOL Warn::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	for (int i = 0; i < 4; i++)
	{
		for (int j = -1; j < 30; j++)
		{
			((CComboBox*)GetDlgItem(IDC_WarnIO0 + i))->InsertString(j,CString(std::to_wstring(j).c_str()));
		}
		((CComboBox*)GetDlgItem(IDC_WarnIO0 + i))->SetCurSel(0);
		((CButton*)GetDlgItem(IDC_Invert0 + i))->SetCheck(false);
		//GetDlgItem(IDC_Information0 + i)->SetWindowTextW(_T(""));
	}
	//绑定错误
	warnMap.SetAt(1, _T("随动误差超限告警\r\n"));
	warnMap.SetAt(2, _T("与远程轴通讯出错\r\n"));
	warnMap.SetAt(3, _T("远程驱动器报错\r\n"));
	warnMap.SetAt(4, _T("正向硬限位\r\n"));
	warnMap.SetAt(5, _T("反向硬限位\r\n"));
	warnMap.SetAt(6, _T("找原点中\r\n"));
	warnMap.SetAt(8, _T("随动误差超限出错\r\n"));
	warnMap.SetAt(9, _T("超过正向软限位\r\n"));
	warnMap.SetAt(10, _T("超过负向软限位\r\n"));
	warnMap.SetAt(11, _T("CANCEL执行中\r\n"));
	warnMap.SetAt(12, _T("脉冲频率超过MAX_SPEED限制.需要修改降速或修改MAX_SPEED\r\n"));
	warnMap.SetAt(22, _T("告警信号输入\r\n"));
	warnMap.SetAt(23, _T("轴进入了暂停状态\r\n"));
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void Warn::OnBnClickedOpenwarn()
{
	// TODO: 在此添加控件通知处理程序代码
	CString name;
	GetDlgItem(IDC_OpenWarn)->GetWindowTextW(name);
	if (name=="开启")
	{
		for (int i = 0; i < 4; i++)
		{
			int nIndex=((CComboBox*)GetDlgItem(IDC_WarnIO0 + i))->GetCurSel();
			CString selectName;
			((CComboBox*)GetDlgItem(IDC_WarnIO0 + i))->GetLBText(nIndex, selectName);
			//设置IO
			PMC_SetAlmIn(Data::handle, i, _ttoi(selectName));
			PMC_SetInvertIn(Data::handle, i, ((CButton*)GetDlgItem(IDC_Invert0 + i))->GetCheck() ? 1 : 0);
		}
		GetDlgItem(IDC_OpenWarn)->SetWindowTextW(_T("关闭"));
		SetTimer(1, 200,NULL);
		
	}
	else
	{
		KillTimer(1);
		GetDlgItem(IDC_OpenWarn)->SetWindowTextW(_T("开启"));
	}
}


void Warn::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	int errorNum = 0;
	for (int i = 0; i < 4; i++)
	{
		PMC_GetAxisStatus(Data::handle, i, &errorNum);
		if (errorNum>0&&errorNum!=lastError[i])
		{
			for (int j = 0; j < 24; j++)
			{
				if ((j > 0 && j < 6) || (j > 7 && j < 11) || j == 12 || j == 22) {
					if (GetBitValue(errorNum,(USHORT)j))
					{
						lastError[i] = errorNum;
						CString warnStr;
						warnMap.Lookup(j, warnStr);
						CString axis;
						axis.Format(_T("AXIS%d轴"),i);
						errorInf[i] = errorInf[i] +axis  + warnStr;
						GetDlgItem(IDC_Information0 + i)->SetWindowTextW(errorInf[i]);
						MessageBox(axis + warnStr);
						
					}
				}
			}
		}
	}


	CDialogEx::OnTimer(nIDEvent);
}

bool Warn::GetBitValue(int errorNum, USHORT bitPos)
{
	return (errorNum & (1 << bitPos))!= 0;
}
	